package com.sundayfactory.workpaychart;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.util.Log;

public class ConnectionUtil {
	private final String TAG = "ConnectionUtil";
	public ConnectionUtil() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * GET 방식
	 * @param uri
	 * @return
	 * @throws Exception
	 */
	public String urlConnectionGet(String uri) throws Exception{
		
	    URL url;//URL 주소 객체
            URLConnection connection;//URL접속을 가지는 객체
            InputStream is;//URL접속에서 내용을 읽기위한 Stream
            InputStreamReader isr;
            BufferedReader br;
       
            try{
                //URL객체를 생성하고 해당 URL로 접속한다..
                Log.d(TAG, uri);
            	
                url = new URL(uri);
            	 
                connection = url.openConnection();
                connection.setConnectTimeout(10000);
                //내용을 읽어오기위한 InputStream객체를 생성한다..
                is = connection.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
    
                //내용을 읽어서 화면에 출력한다..
                String buf = null;
                StringBuffer buffer = new StringBuffer();
                while(true){
                    buf = br.readLine();
                    if(buf == null) break;
                    
                    
                   
                    
                    buffer.append(buf);
                }
                //Monkey3Application.trace_e( buffer.toString(), TAG);
                Log.d(TAG, buffer.toString());
                return buffer.toString();
           
            } catch(MalformedURLException mue) {
                Log.d(this.getClass().getSimpleName(), "잘못된 URL입니다. 사용법 : java URLConn http://hostname/path]");
                mue.printStackTrace();
            } catch(IOException ioe) {
               ioe.printStackTrace();
            }
        return null;
	}
	/**
	 * Post 방식
	 * @param userid
	 * @param mid
	 * @param seq
	 * @param idx
	 * @return
	 * @throws Exception
	 */
	public String urlConnectionStringPost(String url , List<NameValuePair> params) throws Exception{
		
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(url);
		//request.setHeader("Expect", "100-continue");
		request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		HttpResponse res = client.execute(request);
		HttpEntity entity = res.getEntity();
		if (entity != null) {
			InputStream in = entity.getContent();
			byte[] recvByte = new byte[1024];
			String strRecv = "";
			while (true) {
				int readSize = in.read(recvByte, 0, 1024);
				if (readSize <= 0)
					break;
				strRecv += new String(recvByte);
			}
			in.close();
			strRecv = strRecv.trim();
			Log.e(this.getClass().getSimpleName(), url);
			Log.d(this.getClass().getSimpleName(), strRecv);
			return strRecv;
		}
		
		return "false";
			
	}
	
	
	
	
}
